/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.gui;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import java.net.*;
import javax.swing.*;

import blueprint4j.Constants;

public class AnimatingButton extends JButton implements Animation, MouseListener {
    private int fps = 10;
    private boolean animating = false;
    private Icon[] images;
    private int frame=0;
    
    public AnimatingButton(String p_name,String[] p_animation,int p_fps)
    throws IOException{
        super(p_name);
        addMouseListener(this);
        load(p_animation);
        fps=p_fps;
    }
    
    public void addNotify(){
        super.addNotify();
        if (images.length>1){
            AnimationThread.add(this);
        }
    }
    
    public void removeNotify(){
        super.removeNotify();
        if (images.length>1){
            AnimationThread.remove(this);
        }
    }
    
    private void load(String[] resource_name)
    throws IOException {
        images=Gui.loadIconsFromResource(resource_name);
        // Set first icon
        setIcon(images[0]);
    }
    
    public boolean isAnimating() {
        return animating;
    }
    
    public int animationFPS() {
        return fps;
    }
    
    public void animate() {
        frame=(frame+1)%images.length;
        setIcon(images[frame]);
    }
    
    
    public void mouseEntered(MouseEvent e) {
        animating = true;
    }
    
    public void mouseExited(MouseEvent e) {
        animating = false;
    }
    
    public void mouseClicked(MouseEvent e) {}
    public void mousePressed(MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {}
}

